﻿#include <QMessageBox>
#include <QMouseEvent>
#include <QPainter>
#include <ctime>
#include "mainscene.h"
#include "config.h"

MainScene::MainScene(QWidget *parent)
    : QWidget(parent)
{
    initScene();
}

MainScene::~MainScene()
{
}

void MainScene::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.drawPixmap(0,m_map.getMap1PostionY(),m_map.getMap1());
    painter.drawPixmap(0,m_map.getMap2PostionY(),m_map.getMap2());
    painter.drawPixmap(m_plane.getPostionX(),m_plane.getPostionY(),m_plane.getPlane());

    for(int i = 0;i<BULLET_NUM;i++){
         if(!m_plane.bulletsOne[i].getIsFree()){
             painter.drawPixmap(m_plane.bulletsOne[i].getPostionX(),m_plane.bulletsOne[i].getPostionY(),m_plane.bulletsOne[i].getBullet());
         }
    }

    for(int i = 0;i<ENEMY_NUM;i++){
         if(!ememyScenes[i].getIsFree()){
             painter.drawPixmap(ememyScenes[i].getPostionX(),ememyScenes[i].getPostionY(),ememyScenes[i].getEnemy());
         }
    }

    for(int i = 0;i<BOMB_NUM;i++){
        if(!bombs[i].getIsFree()){
            if(!ememyScenes[i].getIsFree()){
                painter.drawPixmap(bombs[i].getPostionX(),bombs[i].getPostionY(),bombs[i].bombAttr[bombs[i].getPicIndex()]);
            }
        }
    }

}

void MainScene::mouseMoveEvent(QMouseEvent *event)
{
   int x = event->x() - m_plane.getPlane().width()*0.5,y =event->y()-m_plane.getPlane().height()*0.5;

   m_plane.updatePostion(x,y);

}

void MainScene::initScene()
{
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    setWindowTitle(tr("打飞机"));
    m_timer.setInterval(10);
    ememyShowCount = 0;
    playGame();
    srand((unsigned int)time(NULL));
}

void MainScene::playGame()
{
    m_timer.start();
    connect(&m_timer,&QTimer::timeout,[=](){
        enemyScene();
        updatePostion();
        update();
        collisionDetection();
    });

}

void MainScene::updatePostion()
{
   m_map.updatePostion();
   m_plane.shoot();

   for(int i = 0;i<BULLET_NUM;i++){
        if(!m_plane.bulletsOne[i].getIsFree()){
            m_plane.bulletsOne[i].updatePostion();
        }
   }

   for(int i = 0;i<ENEMY_NUM;i++){
       if(!ememyScenes[i].getIsFree()){
           ememyScenes[i].updatePostion();
       }
   }

   for(int i = 0;i<BOMB_NUM;i++){
       if(!bombs[i].getIsFree()){
           bombs[i].updateInfo();
       }
   }

}

void MainScene::enemyScene()
{
    ememyShowCount++;

    if(ememyShowCount<ENEMY_INTERVAL){
        return;
    }

    ememyShowCount = 0;

    for(int i = 0;i<ENEMY_NUM;i++){
        if(ememyScenes[i].getIsFree()){
            ememyScenes[i].setIsFree(false);
            ememyScenes[i].setPostionX(rand()%(GAME_WIDTH - ememyScenes[i].getEnemyRect().width()));
            ememyScenes[i].setPostionY(- ememyScenes[i].getEnemyRect().height());
            break;
        }
    }

}

void MainScene::collisionDetection()
{

    for(int j = 0;j<BULLET_NUM;j++){

         if(m_plane.bulletsOne[j].getIsFree()){
                continue;
         }
         for(int i = 0;i<ENEMY_NUM;i++){
              if(ememyScenes[i].getIsFree()){
                 continue;
          }
          if(ememyScenes[i].getEnemyRect().intersects(m_plane.bulletsOne[j].getBulletRect())){
                 ememyScenes[i].setIsFree(true);
                 m_plane.bulletsOne[j].setIsFree(true);
                for(int k=0;k<BOMB_NUM;k++){
                    if(bombs[k].getIsFree()){
                        bombs[k].setIsFree(false);
                         bombs[k].setPostionX(ememyScenes[i].getPostionX());
                         bombs[k].setPostionY(ememyScenes[i].getPostionY());
                         break;
                    }
                }
           }
        }
    }

    for(int i = 0;i<ENEMY_NUM;i++){
         if(ememyScenes[i].getIsFree()){
            continue;
     }
     if(m_plane.getPlaneRect().intersects(ememyScenes[i].getEnemyRect())){

        for(int k=0;k<BOMB_NUM;k++){
             if(bombs[k].getIsFree()){
                bombs[k].setIsFree(false);
                bombs[k].setPostionX(ememyScenes[i].getPostionX());
                bombs[k].setPostionY(ememyScenes[i].getPostionY());
                break;
            }
       }

      }
   }

}

